package com.jxwifi.cloud.quickclean.popwindow;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.idroid.utils.NoDoubleClickListener;
import com.jxwifi.cloud.quickclean.R;
import com.jxwifi.cloud.quickclean.adapter.PopRewardAdapter;
import com.jxwifi.cloud.quickclean.bean.RewardBean;
import com.jxwifi.cloud.quickclean.utils.ToastUtils;
import com.popwindow.BasicPopmodule;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;

public class PopRewardModule extends BasicPopmodule {

    private Context mContext;
    private PopRewardListener mPopRewardtener;

    private EditText mEtPopRewardPrice;

    private PopRewardAdapter mAdapter;
    private int mAreaindex;
    private String mReWardTypePrice;//价格选择
    private String mRewardCode = "";
    private List<RewardBean> mRewardBean;

    public PopRewardModule(Context context,List<RewardBean> mRewardBean, PopRewardListener l) {
        this.mContext = context;
        this.mPopRewardtener = l;
        this.mRewardBean=mRewardBean;

        initView();
    }

    @Override
    public void setPopwindow(PopupWindow pop) {
        mPopwindows = pop;
    }

    @Override
    public View getView() {
        return mContentView;
    }

    private void initView() {
        mContentView = LayoutInflater.from(mContext).inflate(R.layout.pop_reward_module, new LinearLayout(mContext), false);
        setListener();
    }

    public void setListener() {
        View mVwPopBlank = (View) mContentView.findViewById(R.id.vw_pop_blank);
        TextView mTvDelete = (TextView) mContentView.findViewById(R.id.tv_order_starts_cancel_evaluate);
        mEtPopRewardPrice = (EditText) mContentView.findViewById(R.id.et_pop_reward_price);
        final RelativeLayout mRelPopRewardQita=(RelativeLayout)mContentView.findViewById(R.id.rel_pop_reward_qita);
        TextView mTvPopRewardConfirm = (TextView) mContentView.findViewById(R.id.tv_pop_reward_confirm);
        RecyclerView mRvPopRewardList = (RecyclerView) mContentView.findViewById(R.id.rv_pop_reward_list);
        mRvPopRewardList.setLayoutManager(new GridLayoutManager(mContext, 2));
        mRvPopRewardList.setAdapter(mAdapter = new PopRewardAdapter(R.layout.pop_reward_list_item));
        mRewardCode=mRewardBean.get(0).getRewardCode();
        mReWardTypePrice=mRewardBean.get(0).getAmount();
        mRewardBean.get(0).setChechCK(true);
        mAdapter.setNewData(mRewardBean);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                mEtPopRewardPrice.setText(null);
                mRelPopRewardQita.setBackgroundResource(R.drawable.shap_set_meal_nu);
                for (int i=0;i<mAdapter.getData().size();i++){
                    if (i==position){
                        mAdapter.getData().get(i).setChechCK(true);
                    }else{
                        mAdapter.getData().get(i).setChechCK(false);
                    }
                }
                mAdapter.notifyDataSetChanged();
                mRewardCode = mAdapter.getData().get(position).getRewardCode();
                mReWardTypePrice = mAdapter.getData().get(position).getAmount();
            }
        });

        mTvDelete.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                mPopwindows.dismiss();
            }
        });
        mVwPopBlank.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                mPopwindows.dismiss();
            }
        });

        mEtPopRewardPrice.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!TextUtils.isEmpty(s)){
                    mRelPopRewardQita.setBackgroundResource(R.drawable.shap_set_reward);
                    for (int i=0;i<mAdapter.getData().size();i++){
                        mAdapter.getData().get(i).setChechCK(false);
                    }
                    mAdapter.notifyDataSetChanged();
                }else{
                    mRelPopRewardQita.setBackgroundResource(R.drawable.shap_set_meal_nu);
                    for (int i=0;i<mAdapter.getData().size();i++){
                        if (i==0){
                            mAdapter.getData().get(i).setChechCK(true);
                            mRewardCode = mAdapter.getData().get(0).getRewardCode();
                            mReWardTypePrice = mAdapter.getData().get(0).getAmount();
                        }else{
                            mAdapter.getData().get(i).setChechCK(false);
                        }
                    }
                    mAdapter.notifyDataSetChanged();

                }
            }
        });


        mTvPopRewardConfirm.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (!TextUtils.isEmpty(mEtPopRewardPrice.getText().toString())) {
                    if (Double.valueOf(mEtPopRewardPrice.getText().toString()) >0) {
                        mReWardTypePrice = mEtPopRewardPrice.getText().toString();
                        mRewardCode = "";
                        BigDecimal b = new BigDecimal(Double.valueOf(mReWardTypePrice));//金额进行精确计算
                        double DifferencePrice = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                        DecimalFormat format = new DecimalFormat("#0.00");
                        mPopRewardtener.getPayOrder(format.format(DifferencePrice), mRewardCode);
                    } else {
                        Toast toast = Toast.makeText(mContext, "最小金额必须大于0哟！", Toast.LENGTH_SHORT);
                        ToastUtils.showMyToast(toast, 3000);
                    }
                } else { //输入框为空获取选中数据状态
                    mPopRewardtener.getPayOrder(mReWardTypePrice, mRewardCode);
                }
                /*InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE); //得到InputMethodManager的实例
                if (imm.isActive()) {//如果开启
                    imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS);//关闭软键盘，开启方法相同，这个方法是切换开启与关闭状态的
                }*/
                mPopwindows.dismiss();
            }
        });
    }

    public interface PopRewardListener {
        void getPayOrder(String Price, String rewardCode);
    }
}
